package com.example.homecunter.news.newsrepository;

import android.util.Log;

import androidx.lifecycle.MutableLiveData;

import com.example.common.utils.BaseEntity;
import com.example.homecunter.news.newsentity.NewsEntity;
import com.example.homecunter.news.newsentity.NewsType;
import com.example.homecunter.news.newsmodel.NewsModel;
import com.example.homecunter.video.entity.ChannelEntity;
import com.example.homecunter.video.entity.VideoEntity;
import com.example.mvvmcore.repository.BaseRepository;

import org.jetbrains.annotations.NotNull;
import org.reactivestreams.Subscription;

import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.FlowableSubscriber;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class NewsRepository extends BaseRepository<NewsModel> {
    MutableLiveData<BaseEntity<List<NewsType>>> typeLiveData = new MutableLiveData<>();

    MutableLiveData<BaseEntity<List<NewsEntity>>> newsLiveData = new MutableLiveData<>();


    @Override
    protected NewsModel createModel() {
        return new NewsModel();
    }

    public MutableLiveData<BaseEntity<List<NewsType>>> getType(){

        Observable<BaseEntity<List<NewsType>>> type = mModel.getType();

        type.observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io())
                .subscribe(new Observer<BaseEntity<List<NewsType>>>() {
                    @Override
                    public void onSubscribe(@NotNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NotNull BaseEntity<List<NewsType>> listBaseEntity) {
                        typeLiveData.setValue(listBaseEntity);
                    }

                    @Override
                    public void onError(@NotNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });


        return typeLiveData;
    }



    public MutableLiveData<BaseEntity<List<NewsEntity>>> newsData(int newstype,int page){


        Observable<BaseEntity<List<NewsEntity>>> news = mModel.getNews(newstype,page);

        news.observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io())
                .subscribe(new Observer<BaseEntity<List<NewsEntity>>>() {
                    @Override
                    public void onSubscribe(@NotNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NotNull BaseEntity<List<NewsEntity>> listBaseEntity) {
                        newsLiveData.setValue(listBaseEntity);
                    }

                    @Override
                    public void onError(@NotNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
        return newsLiveData;
    }

}
